/**
 * Copyright phonemap.com
 *
 * Licensed under the phonemap.com License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.phonemap.com/licenses/LICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * After Login.
 */
function afterLogin() {
	initialize();
}

//Get avatar path
var getAvatar = function(user) {
	return 'http://graph.facebook.com/'+user.user_id+'/picture';
} 

//Set user
var setProfile = function(user) {
	var avatar = getAvatar(user);
	//set info to avatar
	$('#avatar').attr('src', avatar);
	$('#avatarname').text(user.fullname);
	$('#avataraccount').text(user.username);
	
	//set profile
	if (user.fullname != '' && user.fullname != 'null') {
		$('#fullname').val(user.fullname);
	}
	if (user.gender != '' && user.gender != 'null') {
		$('#gender').val(user.gender);
	}
	
	if (user.link != '' && user.link != 'null') {
		$('#link').val(user.link);
	}
	
	if (user.birthday != '' && user.birthday != 'null') {
		$('#birthday').val(user.birthday);
	}
	
	if (user.interested_in != '' && user.interested_in != 'null') {
		$('#interested_in').val(user.interested_in);
	}
	
	if (user.relationship_status != '' && user.relationship_status != 'null') {
		$('#relationship_status').val(user.relationship_status);
	}
	
	if (user.email != '' && user.email != 'null') {
		$('#email').val(user.email);
	}
	if (user.phone != '' && user.phone != 'null') {
		$('#phone').val(user.phone);
	}
	
	if (user.username != '' && user.username != 'null') {
		$('#username').val(user.username);
	}
	
}

var initialize = function() {
	//load user account from cookie
	var useraccount = $.cookie('useraccount');
	if (useraccount != '' && useraccount != null) {
		$.ajax({
			url : "/search?action=initMap&userId="+useraccount,
			type: 'GET',
	        data: '',
			success : function(resp) {
				if (resp != null) {
					// getting the data from the response object
					var data = resp.data;
					//initialize map
					if(data.length > 0) {
						setProfile(data[0])
					}
				}
			},
			error : function(resp){
				alert('Cannot get data! Please check again');
			}
		});
	} else {//not login
		//Go to index page
		document.location.href = '/index.html';
	}
}